package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;


@RestController
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private HttpSession session;

    //登录
    @PostMapping("/employee/login")
    public ResultInfo login(@RequestBody Employee employee){
        Employee e = employeeService.login(employee.getUsername(),employee.getPassword());
        return ResultInfo.success(e);
    }


    //退出登录  销毁session
    @PostMapping("/employee/logout")
    public ResultInfo logout(){
        session.invalidate();
        return ResultInfo.success();
    }

    //查询所有员工，模糊查询名字
    @GetMapping("/employee/find")
    public ResultInfo find(String name){
        List<Employee> employeeList = employeeService.find(name);
        return ResultInfo.success(employeeList);
    }

    //添加员工
    @PostMapping("/employee")
    public ResultInfo add(@RequestBody Employee employee){
        employeeService.add(employee);
        return ResultInfo.success();
    }

    //回显员工
    @GetMapping("/employee/{id}")
    public ResultInfo findById(@PathVariable Long id){
        Employee emp = employeeService.findById(id);
        return ResultInfo.success(emp);
    }

    //修改员工信息
    @PutMapping("/employee")
    public ResultInfo update(@RequestBody Employee employee){
        employeeService.update(employee);
        return ResultInfo.success();
    }


}
